package leetcode._398;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomPickIndex {
	
	private int[] nums;
	Random r = new Random();

	public RandomPickIndex(int[] nums) {
		this.nums = nums;
	}
	
	public int pick(Integer target) {
		List<Integer> idxs = new ArrayList<Integer>();
		int ng = nums.length;
		for (int i = 0; i < ng; i++) {
			if (target == nums[i]) {
				idxs.add(i);
			}
		}
		
		return idxs.get(r.nextInt(idxs.size()));
	}
	
	public static void main(String[] args) {
		int[] nums = {1, 2, 2, 4, 6, 8, 0, 3, 8, 6};
		
		int index = new RandomPickIndex(nums).pick(2);
		System.out.println("index: " + index);
	}
}
